package com.ccp.dev.workflow.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.engine.FreemarkEngine;
import com.ccp.dev.form.model.FormData;
import com.ccp.dev.form.model.SubTable;
import com.ccp.dev.workflow.dao.NodeMsgTemplateDao;
import com.ccp.dev.workflow.model.NodeMsgTemplate;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassDescribe 流程定义扩展 Service类
 * @author:
 * Date: 2019-08-19
 */


@Service
public class NodeMsgTemplateService  extends BaseService<NodeMsgTemplate> {
    @Resource
    private NodeMsgTemplateDao nodeMsgTemplateDao;
    @Resource
    private FreemarkEngine freemarkEngine;
    /**
     * 解释
     *
     * @param nodeId
     * @param defId
     * @param parentDefId
     * @param formData
     * @return JSONObject	:json.html:是html解释结果 json.text 是text解释结果
     * @exception
     * @since 1.0.0
     */
    public JSONObject parse(String nodeId, String defId, String parentDefId, FormData formData) {
        NodeMsgTemplate nodeMsgTemplate = getObject(nodeId, defId, parentDefId);
        if(nodeMsgTemplate==null){
            //找不到就找全局
            nodeMsgTemplate=getObject("0", defId, parentDefId);
        }
        return parse(nodeMsgTemplate, formData);
    }

    private NodeMsgTemplate getObject(String nodeId, String defId, String parentDefId) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (defId != null) {
            map.put("defId", defId);
        }
        if (nodeId != null) {
            map.put("nodeId", nodeId);
        }
        if (parentDefId != null) {
            map.put("parentDefId", parentDefId);
        }
        return  nodeMsgTemplateDao.getOne(map);
    }
    /**
     * 解释
     *
     * @param nodeMsgTemplate
     * @param formData
     * @return JSONObject :json.html:是html解释结果 json.text 是text解释结果
     * @exception
     * @since 1.0.0
     */
    private JSONObject parse(NodeMsgTemplate nodeMsgTemplate, FormData formData) {
        JSONObject jsonObject = new JSONObject();
        try {

            Map<String, Object> param = new HashMap<String, Object>();

            for (String key : formData.getMainFields().keySet()) {
                // 值转字符串
                param.put(key, formData.getMainFields().get(key) + "");
            }

            for (SubTable subTable : formData.getSubTableList()) {
                List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
                for (Map<String, Object> d : subTable.getDataList()) {
                    // 值转字符串
                    Map<String, String> map = new HashMap<String, String>();
                    for (String key : d.keySet()) {
                        map.put(key, d.get(key) + "");
                    }
                    mapList.add(map);
                }
                param.put(subTable.getTableName() + "s", mapList);
            }

            String html = freemarkEngine.parseByStringTemplate(param, nodeMsgTemplate.getHtml());
            jsonObject.put("html", html);
            String text = freemarkEngine.parseByStringTemplate(param, nodeMsgTemplate.getText());
            jsonObject.put("text", text);
        } catch (Exception e) {
            jsonObject.put("html", "");
            jsonObject.put("text", "");
        }

        return jsonObject;
    }

    /**
     * 根据定义ID获取消息模版
     * @param modelId
     * @return
     */
    public List<NodeMsgTemplate> queryByModelId(String modelId) {
        return nodeMsgTemplateDao.queryByModelId(modelId);
    }
}
